//
//  HeadView.m
//  cm
//
//  Created by suyibaishan on 2017/10/26.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "HeadView.h"

@implementation HeadView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self constructUI];
    }
    return self;
}

-(void)constructUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    UIView *activeView = [[UIView alloc] init];
    activeView.backgroundColor = [UIColor whiteColor];
    [self addSubview:activeView];
    activeView.sd_layout
    .topSpaceToView(self, IS_IPHONE_8?170:IS_IPHONE_8PLUS?190:160)
    .leftSpaceToView(self, 0)
    .widthIs(kScreenWidth)
    .heightIs(180);
    
    NSArray *titleArr = @[@"活动公告",@"车盟商城",@"积分商城"];
    for (NSInteger i = 0; i < titleArr.count; i++) {
        UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        customBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [customBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [customBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        customBtn.tag = 100 + i;
        [customBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [activeView addSubview:customBtn];
        customBtn.sd_layout
        .topSpaceToView(activeView, 5)
        .leftSpaceToView(activeView, i*kScreenWidth/titleArr.count)
        .widthIs(kScreenWidth/titleArr.count)
        .heightIs(35);
    }
    
    _advertView = [[UIImageView alloc] init];
    _advertView.userInteractionEnabled = YES;
//    _advertView.contentMode = UIViewContentModeScaleAspectFill;
//    _advertView.clipsToBounds = YES;
    [activeView addSubview:_advertView];
    _advertView.sd_layout
    .topSpaceToView(activeView, 45)
    .leftSpaceToView(activeView, 10)
    .widthIs(kScreenWidth - 20)
    .heightIs(100);

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(adImageClick)];
    [_advertView addGestureRecognizer:tap];
    
    for (NSInteger i = 0; i < 2; i++) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [activeView addSubview:line];
        line.sd_layout
        .topSpaceToView(activeView, 7.5)
        .leftSpaceToView(activeView, kScreenWidth/3+i*kScreenWidth/3)
        .widthIs(1)
        .heightIs(25);
    }
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:13];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text =  @"超值抢购";
    [activeView addSubview:titleLabel];
    titleLabel.sd_layout
    .topSpaceToView(_advertView, 5)
    .leftSpaceToView(activeView, (kScreenWidth - 100)/2)
    .widthIs(100)
    .heightIs(20);
    
    UIView *bottomline = [[UIView alloc] init];
    bottomline.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self addSubview:bottomline];
    bottomline.sd_layout
    .bottomSpaceToView(self, 8)
    .leftSpaceToView(self, (kScreenWidth - 120)/2)
    .widthIs(120)
    .heightIs(1);
}

-(void)clickButton:(UIButton *)button
{
    _clickBlock(button.tag);
}

- (void)adImageClick
{
    !_adImageClickBlock ? : _adImageClickBlock();
}

@end
